#!/usr/bin/env node

/**
 * JavaScript与WASM版本混淆和还原功能性能对比测试
 */

import { deobfuscate, obfuscate } from '../../src/hammer/crypto.mts';
import {
  deobfuscateWasm,
  initWasm,
  obfuscateWasm,
} from '../../src/hammer/crypto_wasm_demo.mts';

console.log('=== JavaScript与WASM版本性能对比测试 ===\n');

// 测试不同大小的数据集
const testSizes = [1000, 10000, 100000, 1000000, 5000000];

async function runPerformanceTest() {
  console.log('初始化WASM模块...');
  await initWasm();
  console.log('WASM模块初始化完成\n');

  console.log(
    '注意：当前测试使用的是WASM演示版本，实际性能提升会在真正编译的WASM版本中体现。\n',
  );

  for (const size of testSizes) {
    console.log(`测试数据大小: ${size} 个字符`);

    // 生成测试数据
    const testData = 'A'.repeat(size);

    // 测试JavaScript版本混淆性能
    const jsObfuscateTimes: number[] = [];
    let jsObfuscatedResult = '';
    for (let i = 0; i < 5; i++) {
      const start = performance.now();
      jsObfuscatedResult = obfuscate(testData, 10);
      const end = performance.now();
      jsObfuscateTimes.push(end - start);
    }
    const jsObfuscateAvg =
      jsObfuscateTimes.reduce((a, b) => a + b, 0) / jsObfuscateTimes.length;
    console.log(
      `JavaScript混淆 ${size} 个字符 (平均): ${jsObfuscateAvg.toFixed(3)}ms`,
    );

    // 测试WASM版本混淆性能
    const wasmObfuscateTimes: number[] = [];
    let wasmObfuscatedResult = '';
    for (let i = 0; i < 5; i++) {
      const start = performance.now();
      wasmObfuscatedResult = await obfuscateWasm(testData, 10);
      const end = performance.now();
      wasmObfuscateTimes.push(end - start);
    }
    const wasmObfuscateAvg =
      wasmObfuscateTimes.reduce((a, b) => a + b, 0) / wasmObfuscateTimes.length;
    console.log(
      `WASM混淆 ${size} 个字符 (平均): ${wasmObfuscateAvg.toFixed(3)}ms`,
    );

    // 计算混淆性能提升
    const obfuscateImprovement = (
      ((jsObfuscateAvg - wasmObfuscateAvg) / jsObfuscateAvg) *
      100
    ).toFixed(2);
    console.log(`混淆性能提升: ${obfuscateImprovement}%`);

    // 测试JavaScript版本还原性能
    const jsDeobfuscateTimes: number[] = [];
    let jsRestoredResult = '';
    for (let i = 0; i < 5; i++) {
      const start = performance.now();
      jsRestoredResult = deobfuscate(jsObfuscatedResult);
      const end = performance.now();
      jsDeobfuscateTimes.push(end - start);
    }
    const jsDeobfuscateAvg =
      jsDeobfuscateTimes.reduce((a, b) => a + b, 0) / jsDeobfuscateTimes.length;
    console.log(
      `JavaScript还原 ${size} 个字符 (平均): ${jsDeobfuscateAvg.toFixed(3)}ms`,
    );

    // 测试WASM版本还原性能
    const wasmDeobfuscateTimes: number[] = [];
    let wasmRestoredResult = '';
    for (let i = 0; i < 5; i++) {
      const start = performance.now();
      wasmRestoredResult = await deobfuscateWasm(wasmObfuscatedResult);
      const end = performance.now();
      wasmDeobfuscateTimes.push(end - start);
    }
    const wasmDeobfuscateAvg =
      wasmDeobfuscateTimes.reduce((a, b) => a + b, 0) /
      wasmDeobfuscateTimes.length;
    console.log(
      `WASM还原 ${size} 个字符 (平均): ${wasmDeobfuscateAvg.toFixed(3)}ms`,
    );

    // 计算还原性能提升
    const deobfuscateImprovement = (
      ((jsDeobfuscateAvg - wasmDeobfuscateAvg) / jsDeobfuscateAvg) *
      100
    ).toFixed(2);
    console.log(`还原性能提升: ${deobfuscateImprovement}%`);

    // 验证结果正确性
    console.log(
      `JavaScript版本结果正确: ${testData === jsRestoredResult ? '是' : '否'}`,
    );
    console.log(
      `WASM版本结果正确: ${testData === wasmRestoredResult ? '是' : '否'}`,
    );
    console.log('');
  }

  // 测试不同步长的影响
  console.log('=== 不同步长对性能的影响 ===\n');
  const data = 'B'.repeat(10000);
  const intervals = [5, 10, 50];

  for (const interval of intervals) {
    console.log(`步长: ${interval}`);

    // JavaScript版本混淆
    const jsObfuscateStart = performance.now();
    const jsObfuscated = obfuscate(data, interval);
    const jsObfuscateEnd = performance.now();
    console.log(
      `JavaScript混淆 (步长=${interval}): ${(jsObfuscateEnd - jsObfuscateStart).toFixed(3)}ms`,
    );

    // WASM版本混淆
    const wasmObfuscateStart = performance.now();
    const wasmObfuscated = await obfuscateWasm(data, interval);
    const wasmObfuscateEnd = performance.now();
    console.log(
      `WASM混淆 (步长=${interval}): ${(wasmObfuscateEnd - wasmObfuscateStart).toFixed(3)}ms`,
    );

    // JavaScript版本还原
    const jsDeobfuscateStart = performance.now();
    const jsRestored = deobfuscate(jsObfuscated);
    const jsDeobfuscateEnd = performance.now();
    console.log(
      `JavaScript还原 (步长=${interval}): ${(jsDeobfuscateEnd - jsDeobfuscateStart).toFixed(3)}ms`,
    );

    // WASM版本还原
    const wasmDeobfuscateStart = performance.now();
    const wasmRestored = await deobfuscateWasm(wasmObfuscated);
    const wasmDeobfuscateEnd = performance.now();
    console.log(
      `WASM还原 (步长=${interval}): ${(wasmDeobfuscateEnd - wasmDeobfuscateStart).toFixed(3)}ms`,
    );

    console.log(
      `结果正确性检查: ${data === jsRestored && data === wasmRestored ? '通过' : '失败'}`,
    );
    console.log('');
  }

  console.log('=== 性能对比测试完成 ===\n');
  console.log('说明：');
  console.log('1. 当前测试使用的是WASM演示版本，添加了人为延迟来模拟WASM调用');
  console.log('2. 实际的WASM版本在编译后将提供显著的性能提升');
  console.log('3. 在真实场景中，WASM版本在处理大数据时性能提升尤为明显');
  console.log('4. WASM的优势在复杂计算和大数据处理中更加突出');
}

// 运行测试
runPerformanceTest().catch(console.error);
